package com.yyw.foodie.utils;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 易煜玮
 * @ClassName BindingResultJoinError.java
 * @Description 拼接校验结果错误的数据
 * @createTime 2021年05月22日 19:47:00
 */
public class BindingResultJoinError {

    public static Map<String, String> joinErrorMsg(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            Map<String, String> errorMsg = new HashMap<>();
            for (FieldError error : fieldErrors) {
                String message = error.getDefaultMessage();
                String field = error.getField();
                errorMsg.put(field, message);
            }
            return errorMsg;
        }
        return null;
    }
}
